﻿
//-----------------------------------------------------------------------
// <copyright file="Contact.cs" company="FastNET Services">
//     Copyright (c) 2007-2011. All Rights Reserved.
// </copyright>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings
using FastNet.BusinessSystem.Objects;
using FastNet.BusinessSystem.Rules;
using FastNet.RepositorySystem;
using FastNet.RepositorySystem.Interfaces;
using SampleRepository.Entities;
using SampleRepository.Repositories;

#endregion

namespace SampleBusiness.Objects
{
    /// <summary>
    /// Business Object for Contact
    /// </summary>
    public class Contact : IdentityBusinessObject<IContactData, IContactRepository>
    {

        #region CTor/DTor

        /// <summary>
        /// Initializes a new instance of Contact
        /// </summary>
        public Contact()
            : base(new RepositoryFactory())
        {
            AddBusinessRules();
        }

        /// <summary>
        /// Initializes a new instance of Contact
        /// </summary>
        internal Contact(IRepositoryFactory factory, IContactData data)
            : base(factory, data)
        {
            AddBusinessRules();
        }

        #endregion

        #region Business Rules

        /// <summary>
        /// Handles adding of the business rules for validation to the instance.
        /// </summary>
        private void AddBusinessRules()
        {
            BusinessRules.Add(new BusinessLambdaRule("FirstName_IsNotNullOrEmpty", "FirstName", "First name cannot be null or empty", () => !string.IsNullOrEmpty(Data.FirstName)));
            BusinessRules.Add(new BusinessLambdaRule("LastName_IsNotNullOrEmpty", "LastName", "Last name cannot be null or empty", () => !string.IsNullOrEmpty(Data.LastName)));
            BusinessRules.Add(new BusinessLambdaRule("FirstName_LengthMax", "FirstName", "First name cannot be more than 50 characters", () => Data.FirstName != null && Data.FirstName.Length <= 50));
            BusinessRules.Add(new BusinessLambdaRule("LastName_LengthMax", "LastName", "Last name cannot be more than 50 characters", () => Data.LastName != null && Data.LastName.Length <= 50));
            BusinessRules.Add(new BusinessLambdaRule("Phone_LengthMax", "Phone", "Phone cannot be more than 11 characters", () => Data.Phone != null && Data.Phone.Length <= 11));
            BusinessRules.Add(new BusinessLambdaRule("Email_LengthMax", "Email", "Email cannot be more than 255 characters", () => Data.Email != null && Data.Email.Length <= 255));

            BusinessRules.Add(new BusinessLambdaRule("ShippingAddressId_HasValue", "ShippingAddressId", "Shipping address must be associated with a record Id", () => Data.ShippingAddressId != Identity.Empty));
            BusinessRules.Add(new BusinessLambdaRule("BillingAddressId_HasValue", "BillingAddressId", "Billing address must be associated with a record Id", () => Data.BillingAddressId != Identity.Empty));
        }

        #endregion

        #region State Control

        /// <summary>
        /// Gets a value indicating whether or not the current object can be modified
        /// </summary>
        public override bool CanModify { get { return true; } }

        /// <summary>
        /// Gets a value indicating whether or not the current object can be saved
        /// </summary>
        public override bool CanSave { get { return true; } }

        /// <summary>
        /// Gets a value indicating whether or not the current object can be deleted
        /// </summary>
        public override bool CanDelete { get { return true; } }

        #endregion

        public string FirstName
        {
            get { return Data.FirstName; }
            set
            {
                Data.FirstName = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("FirstName"));
            }
        }

        public string LastName
        {
            get { return Data.LastName; }
            set
            {
                Data.LastName = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("LastName"));
            }
        }

        public string Phone
        {
            get { return Data.Phone; }
            set
            {
                Data.Phone = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Phone"));
            }
        }

        public string Email
        {
            get { return Data.Email; }
            set
            {
                Data.Email = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("Email"));
            }
        }

        public Identity ShippingAddressId
        {
            get { return Data.ShippingAddressId; }
            set
            {
                Data.ShippingAddressId = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("ShippingAddressId"));
            }
        }

        public Identity BillingAddressId
        {
            get { return Data.BillingAddressId; }
            set
            {
                Data.BillingAddressId = value;
                OnPropertyChanged(new System.ComponentModel.PropertyChangedEventArgs("BillingAddressId"));
            }
        }

    }
}
